package org.dna.wicketui.issues;

import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.dna.MetroApplication;
import org.dna.UserRoles;
import org.dna.uimodel.MenuItem;
import org.dna.wicketui.MetroPage;
import org.tigris.scarab.model.Issue;
import org.tigris.scarab.model.Module;

public class ManageIssuePage extends MetroPage {

    @UserRoles({"admin", "user"})
    private class DetailsIssueLink extends Link<Void> {

        private static final long serialVersionUID = 4258377978719477979L;

        public DetailsIssueLink(String id) {
            super(id);
        }

        @Override
        public void onClick() {
            switchToPanel(IssueDetailsPanel.class);
        }

        @Override
        public boolean isEnabled() {
            return m_currentPanel instanceof IssueListPanel;
        }
    }

    @UserRoles({"admin", "user"})
    private class ListIssuesLink extends Link<Void> {

        private static final long serialVersionUID = 4258377978719477979L;

        public ListIssuesLink(String id) {
            super(id);
        }

        @Override
        public void onClick() {
            switchToPanel(IssueListPanel.class);
        }

        @Override
        public boolean isEnabled() {
            return m_currentPanel instanceof IssueDetailsPanel;
        }
    }
    private static final Logger LOG = Logger.getLogger(ManageIssuePage.class);
    
    Module m_module;
    
    public ManageIssuePage(PageParameters params) {
        super();
        LOG.info("Creating page with params " + params);
        String modName = params.getString("module", "default");
        m_module = MetroApplication.get().getModuleDAO().findByName(modName);
    }

    /**
     * Not bookmarkable, use to edit an existing Issue
     * */
//    public ManageIssuePage(Issue editable) {
//        m_editMode = true;
//        m_parentActModel = new LoadableActivityModel(editable.getActivity());
//        initComponents(editable);
//    }
    @Override
    protected Panel configurePanels() {
        IModel<List<Issue>> allIssuesModel = new LoadableDetachableModel<List<Issue>>() {

            @Override
            protected List<Issue> load() {
                return MetroApplication.get().getIssueDAO().allIssues();
            }
        };

        IssueListPanel issueListPanel = new IssueListPanel("innerPanel", allIssuesModel);
        IssueDetailsPanel issueDetailsPanel = new IssueDetailsPanel("innerPanel");

        m_panelsMap.put(IssueListPanel.class, issueListPanel);
        m_panelsMap.put(IssueDetailsPanel.class, issueDetailsPanel);
        return issueListPanel;
    }

    @Override
    protected List<MenuItem> preparePageContextMenu() {
        MenuItem newUserItem = new MenuItem("New Issue", ManageIssuePage.class) {

            @Override
            public Component createMenu() {
                DetailsIssueLink link = new DetailsIssueLink("link");
                link.add(new Label("caption", getCaption()));
                return link;
            }
        };

        MenuItem allUsersItem = new MenuItem("All issues", ManageIssuePage.class) {

            @Override
            public Component createMenu() {
                ListIssuesLink link = new ListIssuesLink("link");
                link.add(new Label("caption", getCaption()));
                return link;
            }
        };
        return Arrays.asList(newUserItem, allUsersItem);
    }
}
